package Demo_02;

import com.h817.Yichang.Sqlexception;

import java.sql.*;
import java.util.*;

public class Demojj {
    static final String driver = "com.mysql.cj.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/jdbc? " +
            "serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
    String user = "root";
    String password = "root123";
    Connection  conn = null;
    PreparedStatement pstm = null;
    ResultSet rst = null;

     static  {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() throws SQLException {
         Connection conn = DriverManager.getConnection(url,user,password);
         return conn;
    }
    public PreparedStatement getPreparedStatement(String sql) throws SQLException {
         PreparedStatement pstm = getConnection().prepareStatement(sql);
         return pstm;

    }

    public int executeUpdate(String sql,String...args){

        int row = 0;
        try {
            conn = getConnection();
            pstm = getPreparedStatement(sql);
            if (args!=null){
                for (int i = 0; i < args.length; i++) {
                    pstm.setString(i+1,args[i]);
                }
            }

            row = pstm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                finally {
                    conn=null;
                }
            }
        }
        return row;
    }

    public List<Map<String,Object>> executeQuery(String sql,String...args){
        List list  = new ArrayList();
        try {
            conn = getConnection();
            pstm = getPreparedStatement(sql);
            if (args!=null){
                for (int i = 0; i < args.length; i++) {
                    pstm.setString(i+1,args[i]);
                }
            }

            rst = pstm.executeQuery();
            ResultSetMetaData meta = rst.getMetaData();
            int n = meta.getColumnCount();
            String[] names = new String[n];//存放查询的列名，可能是别名。
            for (int i = 0; i < n; i++) {
                names[i] = meta.getColumnLabel(i+1);

            }
            //ResultSet特点：只读向下，默认指向第一行的上边。
            while (rst.next()){
                Map<String,Object> map = new HashMap<>();
                //放在while里面，在外面是把与key对应的value一遍一遍赋值，取得是同一块地址
                for (int i = 0; i < n; i++) {
//                    Object value = rst.getObject(i+1);
                    String key = names[i];
                    Object value = rst.getObject(key);
                    map.put(key,value);

                }
                list.add(map);
            }



        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                finally {
                    conn=null;
                }
            }
        }
        return list;


    }
    public static void main(String[] args) {
         Demojj sq = new Demojj();
         String sql1 = "SELECT * FROM student";
        List l  = sq.executeQuery(sql1);
        System.out.println(l);

    }
}
